package com.example.jdk7.aio1;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class AIOServer {
    private AsynchronousServerSocketChannel server;
    public AIOServer()throws IOException {
        server = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(8888));
    }
    public void start() throws InterruptedException, ExecutionException, TimeoutException {
        System.out.println("server_start");
        Future<AsynchronousSocketChannel> future = server.accept();
        AsynchronousSocketChannel socket = future.get();
        ByteBuffer readBuf = ByteBuffer.allocate(1024);
        socket.read(readBuf).get(100, TimeUnit.SECONDS);
        System.out.printf("Receiver:%s%n",new String(readBuf.array()));
    }
    public static void main(String args[]) throws Exception{
        new AIOServer().start();
    }
}